<?php
// liste des champs avec leurs propriétés
include_once '../model/modele.php';


if (isset($_POST["submited"])) {
    $listeChampInscription = array(
        'nom' => array(
            'val' => '',
            'regex' => '#^[a-zA-Z -\'éçâêîôûäëïöüÿàèìòùãõñ]+$#',
            'obligatory' => true
        ),
        'prenom' => array(
            'val' => '',
            'regex' => '#^[a-zA-Z -\'éçâêîôûäëïöüÿàèìòùãõñ]+$#',
            'obligatory' => true
        ),
        'adresse1' => array(
            'val' => '',
            'regex' => '',
            'obligatory' => false
        ),
        'adresse2' => array(
            'val' => '',
            'regex' => '#^[a-zA-Z -\'éçâêîôûäëïöüÿàèìòùãõñ]+$#',
            'obligatory' => false
        ),
        'cp' => array(
            'val' => '',
            'regex' => '/^[0-9]{5}/',
            'obligatory' => false
        ),
        'ville' => array(
            'val' => '',
            'regex' => '#^[a-zA-Z -\'éçâêîôûäëïöüÿàèìòùãõñ]+$#',
            'obligatory' => false
        ),
        'telephone' => array(
            'val' => '',
            'regex' => '#^\+{0,1}[1-9]{0,1}[0-9]+$#',
            'obligatory' => false
        ),
        'mail_etu' => array(
            'val' => '',
            'regex' => '#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#',
            'obligatory' => true
        ),
        'mail_perso' => array(
            'val' => '',
            'regex' => '#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#',
            'obligatory' => false
        ),
        'password1' => array(
            'val' => '',
            'regex' => '#^[a-zA-Z -\'éçâêîôûäëïöüÿàèìòùãõñ]+$#',
            'obligatory' => true
        ),
        'password2' => array(
            'val' => '',
            'regex' => '#^[a-zA-Z -\'éçâêîôûäëïöüÿàèìòùãõñ]+$#',
            'obligatory' => true
        )
    );
// vérifications des infos envoyées
    $liste_erreurs = array();
    foreach ($listeChampInscription as $key => $value) {
        $listeChampInscription[$key]['val'] = stripslashes($_POST[$key]);
        $listeChampInscription[$key]['val'] = addslashes($listeChampInscription[$key]['val']);

        if (isset($value['regex'])) {
            // Traitement des regex sauf password
            if (strlen($listeChampInscription[$key]['regex']) != 0) {
                if (strlen($_POST[$key]) != 0) {
                    if (preg_match($value['regex'], $_POST[$key]) <= 0) {
                        $liste_erreurs[$key] = "Erreur Inscription sur :" . $key;
                    }
                } else {
                    if ($value['obligatory']) {
                        $liste_erreurs[$key] = "Champs obligatoire :" . $key;
                    }
                }
            }
        }
    }


    /* SORTIE */
    echo '<div id="output"><fieldset><legend> Sortie Inscription </legend>';
    if (count($liste_erreurs) > 0) {
        echo '<h2> Erreurs </h2>';
        echo '<ul>';
        foreach ($liste_erreurs as $key => $value) {
            echo '<li>' . $key . ' => ' . $value . '</li>';
        }
        echo '</ul>';
    } else {
        // Passage des informations en base
        createUser($listeChampInscription);
        echo '<h2> Création Réussi </h2>';
    }
    echo '</fieldset></div><br/>';
}
?>

<?php ob_start(); ?>

<form method="POST" action="">
    <input type="hidden" name="submited" value="true" />
    <div class="cfg-contactform" id="cfg-contactform-4">
        <div class="cfg-contactform-content">

            <div class="cfg-element-container">
                <div class="cfg-element-set" id="cfg-element-4-9-set" >
                    <div class="cfg-element-content">
                        <span class="cfg-title "  name="titre" id="cfg-element-4-9">Inscription</span>
                    </div>
                </div>
            </div>

            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-14-label" ><span class="cfg-label-value">Nom</span><span class="cfg-required">*</span></label>

                <div class="cfg-element-set" id="cfg-element-4-14-set" >
                    <div class="cfg-element-content">
                        <input type="text" class="cfg-type-text cfg-form-value " name="nom" id="cfg-element-4-14"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-15-label" ><span class="cfg-label-value">Prénom</span><span class="cfg-required">*</span></label>

                <div class="cfg-element-set" id="cfg-element-4-15-set" >
                    <div class="cfg-element-content">
                        <input type="text" class="cfg-type-text cfg-form-value " name="prenom" id="cfg-element-4-15"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-18-label" ><span class="cfg-label-value">Adresse</span></label>

                <div class="cfg-element-set" id="cfg-element-4-18-set" >
                    <div class="cfg-element-content">
                        <input type="text" class="cfg-type-text cfg-form-value " name="adresse1" id="cfg-element-4-18"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-19-label" ><span class="cfg-label-value"> Complément adresse</span></label>

                <div class="cfg-element-set" id="cfg-element-4-19-set" >
                    <div class="cfg-element-content">
                        <input type="text" class="cfg-type-text cfg-form-value " name="adresse2" id="cfg-element-4-19"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-20-label" ><span class="cfg-label-value">Code Postal</span></label>

                <div class="cfg-element-set" id="cfg-element-4-20-set" >
                    <div class="cfg-element-content">
                        <input type="text" class="cfg-type-text cfg-form-value " name="cp" id="cfg-element-4-20"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-21-label" ><span class="cfg-label-value">Ville</span></label>

                <div class="cfg-element-set" id="cfg-element-4-21-set" >
                    <div class="cfg-element-content">
                        <input type="text" class="cfg-type-text cfg-form-value " name="ville" id="cfg-element-4-21"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-22-label" ><span class="cfg-label-value">Téléphone</span></label>

                <div class="cfg-element-set" id="cfg-element-4-22-set" >
                    <div class="cfg-element-content">
                        <input type="text" class="cfg-type-text cfg-form-value " name="telephone" id="cfg-element-4-22"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-25-label" ><span class="cfg-label-value">E-mail étudiant</span><span class="cfg-required">*</span></label>

                <div class="cfg-element-set" id="cfg-element-4-25-set" >
                    <div class="cfg-element-content">
                        <input type="text" class="cfg-type-text cfg-form-value " name="mail_etu" id="cfg-element-4-25"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-26-label" ><span class="cfg-label-value">E-mail personnel</span></label>

                <div class="cfg-element-set" id="cfg-element-4-26-set" >
                    <div class="cfg-element-content">
                        <input type="text" class="cfg-type-text cfg-form-value " name="mail_perso" id="cfg-element-4-26"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-28-label" ><span class="cfg-label-value">Mot de passe</span><span class="cfg-required">*</span></label>

                <div class="cfg-element-set" id="cfg-element-4-28-set" >
                    <div class="cfg-element-content">
                        <input type="password" class="cfg-type-text cfg-form-value " name="password1" id="cfg-element-4-28"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <label class="cfg-label" id="cfg-element-4-29-label" ><span class="cfg-label-value">Vérification mot de passe</span><span class="cfg-required">*</span></label>

                <div class="cfg-element-set" id="cfg-element-4-29-set" >
                    <div class="cfg-element-content">
                        <input type="password" class="cfg-type-text cfg-form-value " name="password2" id="cfg-element-4-29"  />
                    </div>
                </div>

                <div class="cfg-clear"></div>
            </div>


            <div class="cfg-element-container">
                <div class="cfg-element-set" id="cfg-element-4-30-set" >
                    <div class="cfg-element-content">
                        <input type="submit" class="cfg-submit " name="inscription" id="cfg-element-4-30" value="Inscription"   />
                    </div>
                </div>
            </div>

        </div><!-- cfg-contactform-content -->

    </div><!-- cfg-contactform -->

</form> 



<?php $content = ob_get_clean(); ?>
<?php require 'template.php'; ?>


<!--  ANCIEN FORMULAIRE 
<table>
            <form method="POST" action="">

                <input type="hidden" name="submited" value="true" />

                <tr>
                    <td>Nom :</td>
                    <td><input type="text" name="nom" /></td>
                </tr>

                <tr>
                    <td>Prenom :</td>
                    <td><input type="text" name="prenom" /></td>
                </tr>

                <tr>
                    <td>Adresse :</td>
                    <td>
                        <input type="text" name="adresse1" /><br />
                        <input type="text" name="adresse2" />
                    </td>
                </tr>

                <tr>
                    <td>Code Postal :</td>
                    <td><input type="text" name="cp" /></td>
                </tr>

                <tr>
                    <td>Ville :</td>
                    <td><input type="text" name="ville" /></td>
                </tr>

                <tr>
                    <td>Téléphone :</td>
                    <td><input type="text" name="telephone" /></td>
                </tr>

                <tr>
                    <td>Adresse E-Mail Etudiante :</td>
                    <td><input type="text" name="mail_etu" /></td>
                </tr>

                <tr>
                    <td>Adresse E-Mail Personnelle :</td>
                    <td><input type="text" name="mail_perso" /></td>
                </tr>

                <tr>
                    <td>Mot de passe :</td>
                    <td><input type="text" name="password" /></td>
                </tr>

                <tr>
                    <td colspan="2"><input type="submit" value="Inscription" name="inscription" /></td>
                </tr>
            </form>
        </table>
-->